package com.liuhm.dianjuhe.utils;

import com.liuhm.dianjuhe.bean.LatLng;
import com.liuhm.dianjuhe.bean.LatLngBounds;

import java.util.HashMap;
import java.util.Map;

import static java.lang.Math.PI;

/**
 * @author liuhaomin
 * @date 2020/9/15
 */
public class AMapUtils {
    /**
     * 字符串的地图范围变成sql 的范围
     * @param    latLngBounds
     * @return
     */
    public static String geomFromText(LatLngBounds latLngBounds){
        String westSouth=latLngBounds.getSouthwest().longitude+" "+latLngBounds.getSouthwest().latitude;
        String westNorth=latLngBounds.getSouthwest().longitude+" "+latLngBounds.getNortheast().latitude;
        String eastNorth=latLngBounds.getNortheast().longitude+" "+latLngBounds.getNortheast().latitude;
        String eastSouth=latLngBounds.getNortheast().longitude+" "+latLngBounds.getSouthwest().latitude;
        // 西南，西北，东北，东南，西南
        String result="Polygon(("+westSouth+","+westNorth+","+eastNorth+","+eastSouth+","+westSouth+"))";
        return result;
    }
    /**
     * 字符串的地图范围变成sql 的范围
     * @param    bounds
     * @return
     */
    public static String geomFromText(String bounds){
        return geomFromText(boundsToLatLngBounds( bounds));
    }
    /**
     * 字符串的地图范围变成范围
     * @param    bounds       map.getBounds(),
     * @return
     */
    public static LatLngBounds boundsToLatLngBounds(String bounds){
        LatLng northeast=new LatLng(Double.parseDouble(bounds.split(";")[1].split(",")[1]),Double.parseDouble(bounds.split(";")[1].split(",")[0]));
        LatLng southwest=new LatLng(Double.parseDouble(bounds.split(";")[0].split(",")[1]),Double.parseDouble(bounds.split(";")[0].split(",")[0]));
        // 前端map.getBounds()
        LatLngBounds visibleBounds=new LatLngBounds(northeast,southwest);
        return visibleBounds;
    }
    /**
     * 计算两点的距离
     * @param   * @param null
     * @return
     */
    public static float calculateLineDistance(LatLng source, LatLng target) {
        if (source != null && target != null) {
            try {
                double sourceLongitude = source.longitude;
                double sourceLatitude = source.latitude;
                double targetLongitude = target.longitude;
                double targetLatitude = target.latitude;
                double a, b, R;
                R = 6378137;//地球半径
                sourceLatitude = sourceLatitude * Math.PI / 180.0;
                targetLatitude = targetLatitude * Math.PI / 180.0;
                a = sourceLatitude - targetLatitude;
                b = (sourceLongitude - targetLongitude) * Math.PI / 180.0;
                double d;
                double sa2, sb2;
                sa2 = Math.sin(a / 2.0);
                sb2 = Math.sin(b / 2.0);
                d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(sourceLatitude) * Math.cos(targetLatitude) * sb2 * sb2));
                return (float) d;
            } catch (Throwable var30) {
                var30.printStackTrace();
                return 0.0F;
            }
        } else {
            try {
                throw new RuntimeException("非法坐标值");
            } catch (Exception e) {
                e.printStackTrace();
                return 0.0F;
            }
        }
    }
    /**
     * 计算两个坐标的距离
     * @param source
     * @param target
     * @return
     */
  /*  public static float calculateLineDistance(LatLng source, LatLng target) {
        if (source != null && target != null) {
            try {
                double sourceLongitude = source.longitude;
                double sourceLatitude = source.latitude;
                double targetLongitude = target.longitude;
                double targetLatitude = target.latitude;
                sourceLongitude *= 0.01745329251994329D;
                sourceLatitude *= 0.01745329251994329D;
                targetLongitude *= 0.01745329251994329D;
                targetLatitude *= 0.01745329251994329D;
                double var10 = Math.sin(sourceLongitude);
                double var12 = Math.sin(sourceLatitude);
                double var14 = Math.cos(sourceLongitude);
                double var16 = Math.cos(sourceLatitude);
                double var18 = Math.sin(targetLongitude);
                double var20 = Math.sin(targetLatitude);
                double var22 = Math.cos(targetLongitude);
                double var24 = Math.cos(targetLatitude);
                double[] var26 = new double[3];
                double[] var27 = new double[3];
                var26[0] = var16 * var14;
                var26[1] = var16 * var10;
                var26[2] = var12;
                var27[0] = var24 * var22;
                var27[1] = var24 * var18;
                var27[2] = var20;
                double var28 = Math.sqrt((var26[0] - var27[0]) * (var26[0] - var27[0]) + (var26[1] - var27[1]) * (var26[1] - var27[1]) + (var26[2] - var27[2]) * (var26[2] - var27[2]));
                return (float)(Math.asin(var28 / 2.0D) * 1.27420015798544E7D);
            } catch (Throwable var30) {
                var30.printStackTrace();
                return 0.0F;
            }
        } else {
            try {
                throw new RuntimeException("非法坐标值");
            } catch (Exception e) {
                e.printStackTrace();
                return 0.0F;
            }
        }
    }*/
    /**
      * 根据中心点 和 距离算出方格  东北和西南的经纬度
      * @param   source
      * @param   raidus 单位米
      * @return
      */
    public static LatLngBounds getAround(LatLng source, Double raidus) {
        Map map = new HashMap();
        Double latitude = source.latitude;
        Double longitude = source.longitude;

        Double degree = (24901 * 1609) / 360.0;
        double raidusMile = raidus;

        Double dpmLat = 1 / degree;
        Double radiusLat = dpmLat * raidusMile;
        Double minLat = latitude - radiusLat;
        Double maxLat = latitude + radiusLat;

        Double mpdLng = degree * Math.cos(latitude * (PI / 180));
        Double dpmLng = 1 / mpdLng;
        Double radiusLng = dpmLng * raidusMile;
        Double minLng = longitude - radiusLng;
        Double maxLng = longitude + radiusLng;
        LatLng northeast=new LatLng(maxLat,maxLng);
        LatLng southwest=new LatLng(minLat,minLng);
        LatLngBounds latLngBounds=new LatLngBounds(northeast,southwest);
        return latLngBounds;
    }
}
